package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import selenium.WebDriverWrapper;
import sun.rmi.runtime.Log;
import utils.Log4Test;

import java.security.Key;

/**
 * Created by sergey on 03.08.2014.
 */
public class PickYourRingPage extends BasicPage {
    private static final By ADD_TO_CART_FIRST_PRODUCT_LOCATOR = By.xpath("//a[@href='http://www.freddydiamonds.com/cheap-engagement-rings/solitaires/us-enr8027.html']/ancestor::li/div[@class='actions']/button");
    private static final String METAL_TYPE_SELECTOR_LOCATOR = "//select[@name='options[%d]']";
    private static final String METAL_TYPE_LOCATOR = "//select[@name='options[%d]']/option[@value='%d']";
    private static final By RING_SIZE_INPUT_LOCATOR = By.xpath("//input[@class='input-text required-entry  product-custom-option']");
    private static final By ADD_TO_CART_RING_WITH_SIZE_LOCATOR = By.xpath("//button[@title='Add to Cart']");
    private static final String CART_URL = "https://www.freddydiamonds.com/index.php/checkoutcart/checkout_cart/";
    private static final By QUANTITY_OF_RINGS_LOCATOR = By.xpath("//input[@id='qty']");
    public PickYourRingPage(WebDriverWrapper driver)
    {
        super (driver);
        URL = "http://www.freddydiamonds.com/cheap-engagement-rings.html";
        driver.get(URL);

    }
    public void addRingToCart()
    {
        driver.findElement(ADD_TO_CART_FIRST_PRODUCT_LOCATOR).click();
    }
    public void selectMetalTypeSelector(int optionsId, int optionValue)
    {
        driver.findElement(By.xpath(String.format(METAL_TYPE_SELECTOR_LOCATOR,optionsId))).click();
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(METAL_TYPE_LOCATOR,optionsId,optionValue))));

    }
    public void selectMetalType(int optionsId, int optionValue)
    {
        driver.findElement(By.xpath(String.format(METAL_TYPE_LOCATOR,optionsId,optionValue))).click();
        Log4Test.info("Metal type has been selected");
    }
    public void enterRingSizeText(String sizeOfRing)
    {
        driver.findElement(RING_SIZE_INPUT_LOCATOR).sendKeys(sizeOfRing);
        Log4Test.info("Ring size has been written");
    }
    public void addRingWithSizeToCart()
    {
        driver.findElement(ADD_TO_CART_RING_WITH_SIZE_LOCATOR).click();
        Log4Test.info("Ring with selected metal type and size has been selected");
    }
    public void inputQuantityOfRings(String quantity)
    {
        driver.findElement(QUANTITY_OF_RINGS_LOCATOR).click();
        driver.findElement(QUANTITY_OF_RINGS_LOCATOR).sendKeys(Keys.chord(Keys.CONTROL,"a",Keys.DELETE));
        driver.findElement(QUANTITY_OF_RINGS_LOCATOR).sendKeys(quantity);
        Log4Test.info("Quantity of rings has been selected");
    }
    public boolean checkProceedingToCart()
    {
        Log4Test.info("Order has been made");
        return driver.getCurrentUrl().equals(CART_URL);
    }
}
